﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LingDong.IndexReader;

namespace LingDong.WordCluster
{
    class IndexCache
    {
        private IndexCache()
        {
        }

        public static void Init(Index index)
        {
            InitNounDictionary(index);
            InitTotalSentenceCount(index);
            //InitTermDocList(index);
        }

        private static void InitTermDocList(Index index)
        {
            TermDocList = new Dictionary<string, List<int>>();
            foreach (string term in NounDictionary.Keys)
            {
                TermDocList[term] = index.GetTermAppearDocList(term);
            }
        }

        public static int GetTermOccurrenceNumber(string term)
        {
            return NounDictionary[term];
        }

        public static bool ContainsNoun(string term)
        {
            return NounDictionary.ContainsKey(term);
        }

        private static void InitNounDictionary(Index index)
        {
            int maxTid = index.GetTotalTermCount();

            NounDictionary = new Dictionary<string, int>();
            for (int i = 0; i < maxTid; i++)
            {
                string term = index.GetTermByID(i);
                PanGu.POS pos = index.GetTermPOS(term);
                if (pos == PanGu.POS.POS_D_S ||
                    pos == PanGu.POS.POS_D_N ||
                    pos == PanGu.POS.POS_A_NZ ||
                    pos == PanGu.POS.POS_A_NT ||
                    pos == PanGu.POS.POS_A_NS ||
                    pos == PanGu.POS.POS_A_NR)
                {
                    int occur = index.GetTermFrequence(term);
                    NounDictionary.Add(term, occur);
                }
            }
        }

        private static void InitTotalSentenceCount(Index index)
        {
            TotalSentenceCount = 0;
            int maxDid = index.GetTotalDocumentCount();
            for (int i = 0; i < maxDid; i++)
            {
                TotalSentenceCount += index.GetParsedDocument(i).SentenceList.Count();
            }
        }

        public static Dictionary<string, int> NounDictionary { private set; get; }

        public static Dictionary<string, List<int>> TermDocList { private set; get; }

        public static int TotalSentenceCount { private set; get; }
    }
}
